
package com.hc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hc.dao.IEmployeeDao;
import com.hc.domain.Employee;
import com.hc.util.LoggerUtil;
import com.hc.util.StringUtil;

import javassist.compiler.SyntaxError;

@Repository
public class EmployeeDaoImpl extends BaseDaoImpl<Employee> implements IEmployeeDao {

	@Override
	public Employee getByAccount(String account) {
		return super.get("FROM Employee e WHERE e.account=?", new Object[] { account });
	}

	@Override
	public void saveOrUpdateData(Employee admin) {
		super.saveOrUpdate(admin);
	}

	@Override
	public List<Employee> findEmployeePages(Integer roleId, String name, String tel, Integer status, Integer index, Integer count) {
		StringBuffer sql = new StringBuffer();
		if (roleId != null && roleId != 0) {
			sql.append(
					"select e.id, e.account, e.pass, e.salt, e.createtime, e.updatetime, e.tel, e.email, e.sex,e.lastLoginIp, e.lastLoginDate, e.realName,e.headImg, e.loginCount, e.status  FROM employee e, user_and_role u WHERE u.roleId=? AND u.adminId=e.id ");
		} else {
			roleId = null;
			sql.append(
					"select e.id, e.account, e.pass, e.salt, e.createtime, e.updatetime, e.tel, e.email, e.sex,e.lastLoginIp, e.lastLoginDate, e.realName, e.headImg, e.loginCount, e.status FROM employee e WHERE 1=1 ");
		}
		if (!StringUtil.isEmpty(name)) {
			sql.append("AND e.realName=?");
		} else {
			name = null;
		}
		if (!StringUtil.isEmpty(tel)) {
			sql.append("AND e.tel=?");
		} else {
			tel = null;
		}
		if (status!=null){
			sql.append(" AND e.status=?");
		}
		int begin = (index - 1) * count;
		sql.append(" ORDER BY e.id ASC LIMIT " + begin + ", " + count);
		try {
			return super.executeSqlAuto(Employee.class, sql.toString(), new Object[] { roleId, name, tel, status});
		} catch (Exception e) {
			LoggerUtil.error(e);
			return null;
		}
	}

	@Override
	public int getEmployeeTotal() {
		Long count = super.count("SELECT count(e.id) From Employee e");
		if (count == null) {
			return 0;
		}
		return count.intValue();
	}

	@Override
	public Employee getById(Integer id) {
		return super.get("FROM Employee e WHERE e.id=?", new Object[]{id});
		
	}

}
